package com.goodlearning.gloriamobile.activity;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.codec.binary.Base64;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

import com.goodlearning.gloriamobile.R;
import com.goodlearning.gloriamobile.adapter.CustomAdapter;
import com.goodlearning.gloriamobile.adapter.CustomAdapter.IListItemController;
import com.goodlearning.gloriamobile.commons.Utils;
import com.goodlearning.gloriamobile.dto.GetAttendListDto;
import com.goodlearning.gloriamobile.net.JsonServiceRequester;
import com.goodlearning.gloriamobile.net.JsonWrapper;
import com.goodlearning.gloriamobile.net.JsonRequestThread.INetSuccessCallback;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
/**
 * 진행중 강의 출결 확인
 * @author rapty54
 *
 */
public class AdministrationInProgress extends BaseActivity {
	
	private JSONArray jsonArray;
	
	ArrayList<GetAttendListDto> attendListDtos;
	
	CustomAdapter<GetAttendListDto> customAdapter;
	
	ListView administrationListView;
	
	GetAttendListDto getAttendListDto;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.administration_in_progress_body_layout);
		init();
	}

	public void init() {
		baseTitleLeftBtn.setVisibility(View.VISIBLE);
		baseTitleLeftBtn.setTextColor(getResources().getColor(R.color.administration_fifth_text_change));
		baseTitleLeftBtn.setText(R.string.btn_char1_shift_1);
		baseTitleLeftBtn.setTextSize(20.0f);
		baseTitleTv.setText("진행중 강의 출결확인");
		baseTitleRightBtn.setVisibility(View.INVISIBLE);
		administrationListView=(ListView)findViewById(R.id.administration_result_ListView);
		requestInProgress();
	}
	
	public void requestInProgress(){
		
		JsonServiceRequester.requestGetAttendList(AdministrationInProgress.this, new String(Base64.encodeBase64(getUserId().getBytes())), "0", new INetSuccessCallback() {
			
			@Override
			public void sendCallback(JsonWrapper jsonWrapper) {
				
				jsonArray=jsonWrapper.getRootArray();
				
				attendListDtos =new ArrayList<GetAttendListDto>();
				
				for(int i=0;i<jsonArray.size();i++){
					
					JSONObject jsonObject = (JSONObject) jsonArray.get(i);
					
					getAttendListDto=new GetAttendListDto();
					
					getAttendListDto.setDamdangGyosooMid(Utils.convertObjToString(jsonObject.get("damdangGyosooMid")));
					getAttendListDto.setLbNm(Utils.convertObjToString(jsonObject.get("LbNm")));
					getAttendListDto.sethYear(Utils.convertObjToString(jsonObject.get("hYear")));
					getAttendListDto.setLbIdx(Utils.convertObjToString(jsonObject.get("LbIdx")));
					getAttendListDto.settNm(Utils.convertObjToString(jsonObject.get("tNm")));
					getAttendListDto.setLecNm(Utils.convertObjToString(jsonObject.get("lecNm")));
					getAttendListDto.setHakgiNm(Utils.convertObjToString(jsonObject.get("hakgiNm")));
					getAttendListDto.setHbNm(Utils.convertObjToString(jsonObject.get("HbNm")));
					getAttendListDto.setMidSurvey(Utils.convertObjToString(jsonObject.get("midSurvey")));
					getAttendListDto.setEgNm(Utils.convertObjToString(jsonObject.get("egNm")));
					getAttendListDto.setsIdx(Utils.convertObjToString(jsonObject.get("sIdx")));
					getAttendListDto.setLIdx(Utils.convertObjToString(jsonObject.get("LIdx")));
					getAttendListDto.setFinalSurvey(Utils.convertObjToString(jsonObject.get("finalSurvey")));
					getAttendListDto.setJongGangDay(Utils.convertObjToString(jsonObject.get("jongGangDay")));
					getAttendListDto.setGaeGangDay(Utils.convertObjToString(jsonObject.get("gaeGangDay")));
					getAttendListDto.setNm(Utils.convertObjToString(jsonObject.get("nm")));
					getAttendListDto.setEgCd(Utils.convertObjToString(jsonObject.get("egCd")));
					
					attendListDtos.add(getAttendListDto);
					
				}
				
				setVisibleEmptyArea(attendListDtos.size()==0);
				customAdapter=new CustomAdapter<GetAttendListDto>(AdministrationInProgress.this, R.layout.administration_in_progress_row, attendListDtos, listItemController);
				administrationListView.setAdapter(customAdapter);
				administrationListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

					@Override
					public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
							Utils.log("========================item Click=======================");
								GetAttendListDto getAttendListDto=new GetAttendListDto();
								
								getAttendListDto.setDamdangGyosooMid(((GetAttendListDto)parent.getItemAtPosition(position)).getDamdangGyosooMid());
								getAttendListDto.setLbNm(((GetAttendListDto)parent.getItemAtPosition(position)).getLbNm());
								getAttendListDto.sethYear(((GetAttendListDto)parent.getItemAtPosition(position)).gethYear());
								getAttendListDto.setLbIdx(((GetAttendListDto)parent.getItemAtPosition(position)).getLbIdx());
								getAttendListDto.settNm(((GetAttendListDto)parent.getItemAtPosition(position)).gettNm());
								getAttendListDto.setLecNm(((GetAttendListDto)parent.getItemAtPosition(position)).getLecNm());
								getAttendListDto.setHakgiNm(((GetAttendListDto)parent.getItemAtPosition(position)).getHakgiNm());
								getAttendListDto.setHbNm(((GetAttendListDto)parent.getItemAtPosition(position)).getHbNm());
								getAttendListDto.setMidSurvey(((GetAttendListDto)parent.getItemAtPosition(position)).getMidSurvey());
								getAttendListDto.setEgNm(((GetAttendListDto)parent.getItemAtPosition(position)).getEgNm());
								getAttendListDto.setsIdx(((GetAttendListDto)parent.getItemAtPosition(position)).getsIdx());
								getAttendListDto.setLIdx(((GetAttendListDto)parent.getItemAtPosition(position)).getLIdx());
								getAttendListDto.setFinalSurvey(((GetAttendListDto)parent.getItemAtPosition(position)).getFinalSurvey());
								getAttendListDto.setJongGangDay(((GetAttendListDto)parent.getItemAtPosition(position)).getJongGangDay());
								getAttendListDto.setGaeGangDay(((GetAttendListDto)parent.getItemAtPosition(position)).getGaeGangDay());
								getAttendListDto.setNm(((GetAttendListDto)parent.getItemAtPosition(position)).getNm());
								getAttendListDto.setEgCd(((GetAttendListDto)parent.getItemAtPosition(position)).getEgCd());
								
								Utils.changeActivityNSendData(AdministrationInProgress.this, AdministrationAgencyPractice.class, Intent.FLAG_ACTIVITY_CLEAR_TOP,"getAttendListDto", (GetAttendListDto)getAttendListDto);
								}
				});
			}
		});
	}
	
	IListItemController<GetAttendListDto> listItemController=new IListItemController<GetAttendListDto>() {

		@Override
		public View controlView(List<GetAttendListDto> items, int position,View convertView, ViewGroup parent) {
			
			GetAttendListDto attendListDto=(GetAttendListDto)items.get(position);
			
			((TextView)convertView.findViewById(R.id.administration_date_of_class_TextView)).setText(attendListDto.getHakgiNm());
			((TextView)convertView.findViewById(R.id.administration_proffessor_TextView)).setText(attendListDto.getNm());
			((TextView)convertView.findViewById(R.id.administration_classtype_TextView)).setText(attendListDto.getLecNm());
			
			return convertView;
		}
	};
	
	@Override
	protected void onResume() {
		super.onResume();
		
	}
	
}
